<template>
	<view class="page-container back-grey-light">
		<view class="prow-lg back-white info-box">
			<text>名称：</text>
			<u-input input-align="right" v-model="name" type="text" :maxlength="20" placeholder="请输入分类名称" placeholder-style="color: #999;font-size: 26rpx;line-height: 40rpx;text-align: right;" />
		</view>
		<view class="prow-lg back-white info-box">
			<text>分类描述：</text>
			<u-input input-align="right" v-model="description" type="text" :maxlength="20" placeholder="请输入分类描述(选填)" placeholder-style="color: #999;font-size: 26rpx;line-height: 40rpx;text-align: right;" />
		</view>
		
		<!-- 提交按钮 -->
		<view class="submit-button-box">
			<view class="submit-button back-primary" @click="submitHandle()">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
				id: '', // 分类id
				name: '', // 分类名称
				description: '', // 分类描述
			};
		},
		onLoad(options) {
			let params = JSON.parse(options.params)
			if (params) {
				this.id = params.id
				this.name = params.name
				this.description = params.description
				uni.setNavigationBarTitle({
					title: '修改分类'
				})
			}
		},
		methods: {
			// 提交处理
			submitHandle() {
				if (!this.name) {
					return uni.showToast({
						icon: 'none',
						title: '未填写分类名字'
					})
				}
				
				let params = {
					name: this.name
				}
				if (this.description) params['description'] = this.description
				if (this.id) params['id'] = this.id
				this.$api.AddGoodsType(params).then(res => {
					console.log('AddGoodsType-success', res)
					if (res.data.code == 200) {
						uni.navigateBack({
							delta: 1,
							success: () => {
								uni.showToast({
									icon: 'none',
									title: this.id?'修改成功！':'添加成功！'
								})
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log('AddGoodsType-fail', err)
					uni.showToast({
						icon: 'none',
						title: this.id?'修改失败！':'添加失败！'
					})
				})
			}
		},
	}
</script>

<style lang="scss">
	// 信息项
	.info-box {
		height: 94rpx;
		display: grid;
		grid-template-columns: 160rpx 1fr;
		align-items: center;
		border-bottom: 2rpx solid #f0f0f0;
		
	}
</style>
